package cn.felord.verse.configure;

import cn.felord.WeComTokenCacheable;
import cn.felord.api.WorkWeChatApi;
import jakarta.validation.constraints.NotNull;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * The type We com sdk configuration.
 *
 * @author felord.cn
 * @since 1.0.14.RELEASE
 */
@Configuration(proxyBeanMethods = false)
public class WeComSdkConfiguration {

    /**
     * Redis we com cacheable redis we com cacheable.
     *
     * @return the redis we com cacheable
     */
    @Bean
    RedisWeComCacheable redisWeComCacheable() {
        return new RedisWeComCacheable();
    }

    /**
     * Work we chat api work we chat api.
     *
     * @param redisWeComCacheable the redis we com cacheable
     * @return the work we chat api
     */
    @Bean
    public WorkWeChatApi workWeChatApi(WeComTokenCacheable redisWeComCacheable) {
        return new WorkWeChatApi(redisWeComCacheable);
    }

    /**
     * The type Redis we com cacheable.
     */
    public static class RedisWeComCacheable implements WeComTokenCacheable {
        private static final String QYWX_TOKEN_CACHE = "token::qywx";
        private static final String QYWX_CORP_TICKET_CACHE = "ticket::qywx::corp";
        private static final String QYWX_AGENT_TICKET_CACHE = "ticket::qywx::agent";

        @CachePut(value = {QYWX_TOKEN_CACHE}, key = "#corpId.concat('::').concat(#agentId)")
        @Override
        public String putAccessToken(@NotNull String corpId, @NotNull String agentId, @NotNull String accessToken) {
            return accessToken;
        }

        @Cacheable(value = {QYWX_TOKEN_CACHE}, key = "#corpId.concat('::').concat(#agentId)")
        @Override
        public String getAccessToken(@NotNull String corpId, @NotNull String agentId) {
            return null;
        }

        @CacheEvict(value = {QYWX_TOKEN_CACHE}, key = "#corpId.concat('::').concat(#agentId)")
        @Override
        public void clearAccessToken(String corpId, String agentId) {

        }

        @CachePut(value = {QYWX_CORP_TICKET_CACHE}, key = "#corpId.concat('::').concat(#agentId)")
        @Override
        public String putCorpTicket(@NotNull String corpId, @NotNull String agentId, @NotNull String corpTicket) {
            return corpTicket;
        }

        @Cacheable(value = {QYWX_TOKEN_CACHE}, key = "#corpId.concat('::').concat(#agentId)")
        @Override
        public String getCorpTicket(@NotNull String corpId, @NotNull String agentId) {
            return null;
        }

        @CachePut(value = {QYWX_AGENT_TICKET_CACHE}, key = "#corpId.concat('::').concat(#agentId)")
        @Override
        public String putAgentTicket(@NotNull String corpId, @NotNull String agentId, @NotNull String agentTicket) {
            return agentTicket;
        }

        @Cacheable(value = {QYWX_TOKEN_CACHE}, key = "#corpId.concat('::').concat(#agentId)")
        @Override
        public String getAgentTicket(@NotNull String corpId, @NotNull String agentId) {
            return null;
        }
    }

}
